<?php

require_once 'phpmoko/externals/defaults.php';
require_once 'phpmoko/ui/Panel.php';
require_once 'phpmoko/utils/Console.php';

class RootPanel extends Panel
{
	private static $uniqueId = 0;
	private static $instance = NULL;
	private $title = null;
	private $language = null;
	private $links = array();
	private $linksFixed = array();
	private $jsFiles = array();
	private $jsFilesFixed = array();
	private $metaTags = array();
	private $errorMessages = array();
	private $warningMessages = array();
	private $debugMessages = array();
	private $infoMessages = array();
	private $jsCode = null;
	private $cssCode = null;
	private $onReadyJs = null;
	private $cache = true;
	private $showLogo = true;
	private $googleAnalyticsId = null;
	private $favicon = null;
	private $combiFiles = false;
	private $mysql = null;

	protected function __construct()
	{
		parent::__construct();
		
		Console::init();

//		$this->addCSSFile('/pmk-ext/reset.css');
	}

	function setMySQL($mysql)
	{
		$this->mysql = $mysql;
	}

	/**
	 * @return MySQL
	 */
	function getMySQL()
	{
		if (!$this->mysql) {
			$callee = current(debug_backtrace());
			echo $callee['file'].' ('.$callee['line'].')<br><br>';
			echo 'Mysql not defined';
			die();
		}
		
		return $this->mysql;
	}

	function setGoogleAnalyticsId($id)
	{
		$this->googleAnalyticsId = $id;
	}

	function setShowLogo($enabled)
	{
		$this->showLogo = $enabled;
	}

	function setCombiFiles($enabled)
	{
		$this->combiFiles = $enabled;
	}

	static function getUniqueId()
	{
		self::$uniqueId++;
		return 'single_'.self::$uniqueId;
	}

	/**
	 * gets the RootPanel
	 * @return RootPanel The current object
	 */
	static function get()
	{
		if (self::$instance == NULL)
			self::$instance = new RootPanel();
		
		return self::$instance;
	}

	function addCSSFile($href, $fixed = false)
	{
		include_once 'phpmoko/ui/Head/Link.php';
		
		$link = new Link();
		$link->setRel('stylesheet');
		$link->setType('text/css');
		$link->setHref($href);
		
		if ($fixed)
			$this->linksFixed[] = $link;
		else
			$this->links[] = $link;
	}

	function addErrorMessage($message)
	{
		$this->errorMessages[] = $message;
	}

	function addWarningMessage($message)
	{
		$this->warningMessages[] = $message;
	}

	function addDebugMessage($message)
	{
		$this->debugMessages[] = $message;
	}

	function addInfoMessage($message)
	{
		$this->infoMessages[] = $message;
	}

	function setFavicon($href)
	{
		include_once 'phpmoko/ui/Head/Link.php';
		
		$this->favicon = new Link();
		$this->favicon->setRel('SHORTCUT ICON');
		$this->favicon->setHref($href);
	}

	function addJSFile($file, $fixed = false)
	{
		if ($fixed)
			$this->jsFilesFixed[] = $file;
		else
			$this->jsFiles[] = $file;
	}

	function addJSCode($code)
	{
		if ($this->jsCode != null)
			$this->jsCode .= ' ';
		
		$this->jsCode .= $code;
	}

	function addCSSCode($code)
	{
		if ($this->cssCode != null)
			$this->cssCode .= ' ';
		
		$this->cssCode .= $code;
	}

	function addJSOnReadyDocument($js)
	{
		if ($this->onReadyJs != null)
			$this->onReadyJs .= ' ';
		
		$this->onReadyJs .= $js;
	}

	function setTitle($title)
	{
		$this->title = $title;
	}

	function setLanguage($language)
	{
		$this->language = $language;
	}

	function setMetaTag($name, $content)
	{
		$this->metaTags[$name] = $content;
	}

	function enableCache($enable)
	{
		$this->cache = $enable;
	}

	function toString()
	{
		$content = '';
		foreach ($this as $widget)
			$content .= $widget->toString();
		
		$str = '<!DOCTYPE html>';
		$str .= '<html';
		if ($this->language != null)
			$str .= ' lang="'.$this->language.'"';

		$str .= '>';
		
		$str .= '<head>';
		if ($this->title != null)
			$str .= '<title>'.$this->title.'</title>';
		
		$str .= '<meta charset="utf-8"/>';

		foreach ($this->metaTags as $name => $value)
			$str .= '<meta name="'.$name.'" content="'.$value.'" />';
		
		if ($this->favicon != null)
			$str .= $this->favicon->toString();
		
		$cssfiles = "";
		$lasttime = mktime(1, 1, 1, 1, 1, 1980);
		for ($i = 0; $i < sizeof($this->links); $i++)
		{
			if ($this->combiFiles && ($this->links[$i]->getType() == "text/css"))
			{
				if ($cssfiles != "")
					$cssfiles .= "&amp;";
				
				$cssfiles .= $this->links[$i]->getHref();
				$date = filemtime('.'.$this->links[$i]->getHref());
				if ($lasttime < $date)
					$lasttime = $date;
			}
			else
				$str .= $this->links[$i]->toString();
		}
		
		if ($this->combiFiles && ($cssfiles != ""))
		{
			$link = new Link();
			$link->setRel('stylesheet');
			$link->setType('text/css');
			$link->setHref('/a/combi'.$cssfiles.'&amp;d='.$lasttime);
			$str .= $link->toString();
		}
		
		for ($i = 0; $i < sizeof($this->linksFixed); $i++)
			$str .= $this->linksFixed[$i]->toString();
		
		$jsfiles = "";
		$lasttime = mktime(1, 1, 1, 1, 1, 1980);
		for ($i = 0; $i < sizeof($this->jsFiles); $i++)
		{
			if ($this->combiFiles)
			{
				if ($jsfiles != "")
					$jsfiles .= "&amp;";
				
				$jsfiles .= $this->jsFiles[$i];
				$date = filemtime('.'.$this->jsFiles[$i]);
				if ($lasttime < $date)
					$lasttime = $date;
			}
			else
				$str .= '<script src="'.$this->jsFiles[$i].'" type="text/javascript"></script>';
		}
		
		if ($this->combiFiles && ($jsfiles != ""))
			$str .= '<script src="/a/combi'.$jsfiles.'&amp;d='.$lasttime.'" type="text/javascript"></script>';
		
		for ($i = 0; $i < sizeof($this->jsFilesFixed); $i++)
			$str .= '<script src="'.$this->jsFilesFixed[$i].'" type="text/javascript"></script>';
		
		if ($this->jsCode != null)
			$str .= '<script type="text/javascript">'.$this->jsCode.'</script>';
		
		if ($this->cssCode != null)
			$str .= '<style type="text/css">'.$this->cssCode.'</style>';
		
		$str .= '</head>';
		$str .= '<body>';
		
		if ($this->errorMessages || $this->warningMessages || $this->debugMessages || $this->infoMessages)
		{
			//$str .= '<div style="position: fixed; bottom: 0px; right: 50%;; left: 50%; margin-left: -450px; width:900px">';
			$str .= '<div style="position: fixed; bottom: 0px; left: 0px">';
			if ($this->errorMessages)
			{
				$str .= '<div style="background-color: #FFCCCC; margin: 8px; padding: 8px; border: #DD7777 solid 1px; color: #AA0000">';
				$str .= '<b>Error Message</b><br><br>';
				for ($i = 0; $i < sizeof($this->errorMessages); $i++)
					$str .= $this->errorMessages[$i].'<br>';
				$str .= '</div>';
			}
			
			if ($this->debugMessages)
			{
				$str .= '<div style="background-color: #D2E5C3; margin: 8px; padding: 8px; border: #84DCA8 solid 1px; color: #008800">';
				$str .= '<b>Debug Message</b><br><br>';
				for ($i = 0; $i < sizeof($this->debugMessages); $i++)
					$str .= $this->debugMessages[$i].'<br>';
				$str .= '</div>';
			}
			
			if ($this->infoMessages)
			{
				$str .= '<div style="background-color: #C3D2E5; margin: 8px; padding: 8px; border: #84A8DC solid 1px; color: black">';
				$str .= '<b>Info Message</b><br><br>';
				for ($i = 0; $i < sizeof($this->infoMessages); $i++)
					$str .= $this->infoMessages[$i].'<br>';
				$str .= '</div>';
			}
			
			if ($this->warningMessages)
			{
				$str .= '<div style="background-color: #FFFFCC; margin: 8px; padding: 8px; border: #F0C020 solid 1px; color: black">';
				$str .= '<b>Warning Message</b><br><br>';
				for ($i = 0; $i < sizeof($this->warningMessages); $i++)
					$str .= $this->warningMessages[$i].'<br>';
				$str .= '</div>';
			}
			
			$str .= '</div>';
		}
		
		if ($this->googleAnalyticsId != null)
			$str .= "<script type=\"text/javascript\">\nvar _gaq = _gaq || [];\n_gaq.push(['_setAccount', '".$this->googleAnalyticsId."']);\n_gaq.push(['_trackPageview']);\n(function() {\nvar ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\nga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);\n})();</script>";
		
		$str .= $content;
		
		if ($this->onReadyJs != null)
			$str .= '<script> $(document).ready(function() { '.$this->onReadyJs.' });</script>';
		
		if ($this->showLogo)
			$str .= '<center><a href="http://www.phpmoko.com/"><img src="http://www.phpmoko.com/images/powered_by_phpmoko.png" border="0"></a></center>';
		
		$str .= '</body>';
		$str .= '</html>';
		
		if ($this->cache)
		{
			require_once 'phpmoko/utils/Cache.php';
			$str = Cache::compressStream($str);
		}
		
		return $str;
	}
}
?>
